PRO TABLES_BGPS_MAKE, SUBSET=subset
;;==========================================================================
;; Routine to make tables of the output data from BGPS_FFORE.pro

COMMON FFORE_BLOCK,n,rb3,R0,d,R,Z,tau,f_data,sig_f,corr,farlist,do_Tfit,$
   lpstr,rho_hi,rho_h2,rho_star

COMMON DATA_BLOCK, s,nh2_irdc,nh2_bgps,sig_nb,sig_ni

IF n_elements(rb3) EQ 0 THEN BEGIN
   restore,'./irdc_dist_model/bgps_rb3.sav',/ver
   n = n_elements(rb3)
ENDIF

IF n_elements(subset) EQ 0 THEN subset = ''
suff = ''

;; There are currently 3 cases of the model
nmods     = 3
dfn       = strarr(nmods)

DIR = '/d1/BGPS/bgps_svn/papers/IRDC_prior/tables/'

;; FOR j=3, nmods-1 DO BEGIN
   
;;    openw,lun,DIR+'bgps_params_fixedt'+subset+suff+'_case'+$
;;          string(j+1,format="(I0)")+'.tex',/get_lun
   
   
;; ;; Print Table header to file
;;    printf,lun,'\begin{deluxetable}{ccccccccccccc}'
;;    printf,lun,'  \tablecolumns{13}'
;;    printf,lun,'  \tablewidth{0pc}'
;;    printf,lun,'  \tabletypesize{\scriptsize}'
;;    printf,lun,'  \tablecaption{Parameters and $\chi^2$ values for the \ffore\ model cases with fixed $T_d$\label{table:params_fixedt'+subset+'}}'
;;    printf,lun,'  \tablehead{'
;;    printf,lun,'    \colhead{$T_d$} & \multicolumn{3}{c}{Model Parameters} & \colhead{} & \multicolumn{3}{c}{Normalized Covariance} & \colhead{} & \multicolumn{4}{c}{Fit Statistics} \\'
;;    printf,lun,'    \cline{2-4} \cline{6-8} \cline{10-13} '
;;    printf,lun,'    \colhead{(K)} & \colhead{$A$\tablenotemark{a}} & \colhead{$B$\tablenotemark{b}} & \colhead{$C$\tablenotemark{c}} & \colhead{} &'
;;    printf,lun,'    \colhead{$\rho_{AB}$} & \colhead{$\rho_{AC}$} & \colhead{$\rho_{BC}$} & \colhead{} &'
;;    printf,lun,'    \colhead{$\chi^2_\nu$} & \colhead{$\chi^2_{red}$} &'
;;    printf,lun,'    \colhead{$N_{far}$} & \colhead{$N_{tot}$}'
;;    printf,lun,'  }'
;;    printf,lun,'  \startdata'
   
;; ;; Get DATA!
   
;;    FOR Td = 10., 41., 5. DO BEGIN
      
;;       ;; ;; Temperature header for the 3 sections of the table
;;       ;; printf,lun,'  \hline'
;;       ;; printf,lun,'  \sidehead{$T_d$ = '+string(Td,format="(I0)")+' K}'
;;       ;; printf,lun,'  \hline'
      
      
;;       message,'Case #'+string(j+1,format="(I0)"),/inf
;;       dfn[j] = './irdc_dist_model/data/case'+$
;;                string(j+1,format="(I0)")+'_irdc_Td'+$
;;                string(Td,format="(I0,'K')")+subset+suff+'.sav'
      
;;       restore,dfn[j]
;;       ntot = n_elements(nearfar)
      
;;       A = res[0]
;;       B = res[1]
;;       C = res[2]
;;       G = res[3]
;;       sigA = sqrt(covar[0,0]) > 0.d
;;       sigB = sqrt(covar[1,1]) > 0.d
;;       sigC = sqrt(covar[2,2]) > 0.d
      
;;       ;; Put parameters into strings, to include \nodata for cases
;;       ;; where that parameter is left out.
      
;;       parA = string(A,sigA,format="('$ ',F0.2,' \pm ',F0.2,' $')")
;;       IF (j+1) EQ 6 THEN BEGIN
;;          parB = string(B/1.d3,sigB/1.d3,format="('$ ',F0.2,' \pm ',F0.2,' $')")
;;       ENDIF ELSE $
;;          parB = string(B,sigB,format="('$ ',F0.2,' \pm ',F0.2,' $')")
      
;;       parC = string(C,sigC,format="('$ ',F0.2,' \pm ',F0.2,' $')")
      
;;       IF sigA EQ 0 THEN parA += '\tablenotemark{\dag}'
;;       IF sigB EQ 0 THEN parB += '\tablenotemark{\dag}'
;;       IF sigC EQ 0 THEN parC += '\tablenotemark{\dag}'
      
      
;;       ;; Correlations...
;;       IF FINITE(correl[0,1]) THEN $
;;          rhoAB = string(correl[0,1],format="(F0.3)") ELSE $
;;             rhoAB = '\nodata'
;;       IF FINITE(correl[0,2]) THEN $
;;          rhoAC = string(correl[0,2],format="(F0.3)") ELSE $
;;             rhoAC = '\nodata'
;;       IF FINITE(correl[1,2]) THEN $
;;          rhoBC = string(correl[1,2],format="(F0.3)") ELSE $
;;             rhoBC = '\nodata'
      
;;       chisq_red = bestnorm / double(dof)
      
;;       printf,lun,string(Td,parA,parB,parC,$
;;                         rhoAB,rhoAC,rhoBC,bestnorm,$
;;                         chisq_red,nfar,ntot,format=$
;;                         "('  ',I0,' & ',A0,' & ',A0,' & ',A0,' & & ',A0,' & ',A0,' & ',A0,' & & ',I0,' & ',F0.2,' & ',I0,' & ',I0,' \\')")
      
      
;;    ENDFOR
   
;; ;; Print table footer to file
;;    printf,lun,'  \enddata'
;;    printf,lun,'  \tablenotetext{a}{Foreground veil}'
;;    CASE (j+1) OF
;;       4: BEGIN
;;          printf,lun,'  \tablenotetext{b}{Relative \HI}'
;;          printf,lun,'  \tablenotetext{c}{Relative $\Sigma_*$}'
;;       ENDCASE
;;       5: BEGIN
;;          printf,lun,'  \tablenotetext{b}{Relative \HI}'
;;          printf,lun,'  \tablenotetext{c}{Relative \htwo}'
;;       ENDCASE
;;       6: BEGIN
;;          printf,lun,'  \tablenotetext{b}{Radial Scale Length (kpc)}'
;;          printf,lun,'  \tablenotetext{c}{Vertical Scale Height (pc)}'
;;       ENDCASE
;;       7: BEGIN
;;          printf,lun,'  \tablenotetext{b}{Radial Scale Length (kpc)}'
;;          printf,lun,'  \tablenotetext{c}{Vertical Scale Height (pc)}'
;;       ENDCASE
;;    ENDCASE
;;    printf,lun,'  \tablenotetext{\dag}{Parameter pegged at a limit, quoted uncertainty meaningless}'
;;    printf,lun,'  \tablecomments{Uncertainties on parameters are the formal parameter uncertainties returned by \mtt{MPFIT}, and not the marginalized uncertainties.}'
;;    printf,lun,'\end{deluxetable}'
   
   
;; ;; Clean up
;;    close,lun
;;    free_lun,lun
;; ENDFOR

;; Now, do the Td=FIT case!

openw,lun,DIR+'bgps_params_tdfit'+subset+suff+'.tex',/get_lun


;; Print Table header to file
printf,lun,'\begin{deluxetable}{ccccccccccccccccc}'
printf,lun,'  \tablecolumns{17}'
printf,lun,'  \tablewidth{0pc}'
printf,lun,'  \tabletypesize{\scriptsize}'
printf,lun,'  \rotate'
printf,lun,'  \tablecaption{Parameters and $\chi^2$ values for the \ffore\ model cases with fitted $T_d$\label{table:params_tdfit'+subset+'}}'
printf,lun,'  \tablehead{'
printf,lun,'    \colhead{} & \multicolumn{4}{c}{Model Parameters} & \colhead{} & \multicolumn{6}{c}{Normalized Covariance} & \colhead{} & \multicolumn{4}{c}{Fit Statistics} \\'
printf,lun,'    \cline{2-5} \cline{7-12} \cline{14-17} '
printf,lun,'    \colhead{Case \#} & \colhead{$A$} & \colhead{$B$} & \colhead{$C$} & \colhead{$T_d$ (K)} & \colhead{} &'
printf,lun,'    \colhead{$\rho_{AB}$} & \colhead{$\rho_{AC}$} & \colhead{$\rho_{BC}$} &'
printf,lun,'    \colhead{$\rho_{AT}$} & \colhead{$\rho_{BT}$} & \colhead{$\rho_{CT}$} & \colhead{} &'
printf,lun,'    \colhead{$\chi^2_\nu$} & \colhead{$\chi^2_{red}$} &'
printf,lun,'    \colhead{$N_{far}$} & \colhead{$N_{tot}$}'
printf,lun,'  }'
printf,lun,'  \startdata'

;; Get DATA!


;; Temperature header for the 3 sections of the table
;;printf,lun,'  \sidehead{$T_d$ = '+string(Td,format="(I0)")+' K}'

FOR j=0, nmods-1 DO BEGIN
   
   message,'Case #'+string(j+1,format="(I0)"),/inf
   dfn[j] = './irdc_dist_model/data/case'+$
            string(j+1,format="(I0)")+$
            '_irdc_TdFIT'+subset+suff+'.sav'
   
   restore,dfn[j]
   ntot = n_elements(nearfar)
   
   A = res[0]
   B = res[1]
   C = res[2]
   G = res[3]
   T = res[4]
   sigA = sqrt(covar[0,0]) > 0.d
   sigB = sqrt(covar[1,1]) > 0.d
   sigC = sqrt(covar[2,2]) > 0.d
   sigT = sqrt(covar[4,4]) > 0.d
   
   ;; Put parameters into strings, to include \nodata for cases
   ;; where that parameter is left out.
   
   parA = string(A,sigA,format="('$ ',F0.2,' \pm ',F0.2,' $')")
   IF (j+1) EQ 6 || (j+1) EQ 7 THEN BEGIN
      parB = string(B/1.d3,sigB/1.d3,format="('$ ',F0.2,' \pm ',F0.2,' $')")
   ENDIF ELSE $
      parB = string(B,sigB,format="('$ ',F0.2,' \pm ',F0.2,' $')")
   parC = string(C,sigC,format="('$ ',F0.2,' \pm ',F0.2,' $')")
   
   IF sigA EQ 0 THEN parA += '\tablenotemark{\dag}'
   IF sigB EQ 0 THEN parB += '\tablenotemark{\dag}'
   IF sigC EQ 0 THEN parC += '\tablenotemark{\dag}'
   
   ;; Correlations...
   IF FINITE(correl[0,1]) THEN $
      rhoAB = string(correl[0,1],format="(F0.3)") ELSE $
         rhoAB = '\nodata'
   IF FINITE(correl[0,2]) THEN $
      rhoAC = string(correl[0,2],format="(F0.3)") ELSE $
         rhoAC = '\nodata'
   IF FINITE(correl[1,2]) THEN $
      rhoBC = string(correl[1,2],format="(F0.3)") ELSE $
         rhoBC = '\nodata'
   IF FINITE(correl[0,4]) THEN $
      rhoAT = string(correl[0,4],format="(F0.3)") ELSE $
         rhoAT = '\nodata'
   IF FINITE(correl[1,4]) THEN $
      rhoBT = string(correl[1,4],format="(F0.3)") ELSE $
         rhoBT = '\nodata'
   IF FINITE(correl[2,4]) THEN $
      rhoCT = string(correl[2,4],format="(F0.3)") ELSE $
         rhoCT = '\nodata'
   
   
   chisq_red = bestnorm / double(dof)
   
   printf,lun,string(j+1,parA,parB,parC,T,sigT,rhoAB,rhoAC,rhoBC,$
                     rhoAT,rhoBT,rhoCT,bestnorm,chisq_red,nfar,ntot,format=$
                     "('  ',I0,' & ',A0,' & ',A0,' & ',A0,' & $',F0.1,' \pm ',F0.1,'$ & & ',A0,' & ',A0,' & ',A0,' & ',A0,' & ',A0,' & ',A0,' & & ',I0,' & ',F0.2,' & ',I0,' & ',I0,' \\')")
   
   

ENDFOR
;; Print table footer to file
printf,lun,'  \enddata'
printf,lun,'  \tablenotetext{\dag}{Parameter pegged at a limit, quoted uncertainty meaningless}'
printf,lun,'  \tablecomments{Uncertainties on parameters are the formal parameter uncertainties returned by \mtt{MPFIT}, and not the marginalized uncertainties.}'
printf,lun,'  \tablecomments{\cnote This table will vanish in the final version -- present for now for reference.}'
printf,lun,'\end{deluxetable}'


;; Clean up
close,lun
free_lun,lun



;;===============================================
;; SAMPLE DATA SET

openw,lun,DIR+'source_list.tex',/GET_LUN
IF n_elements(rb3) EQ 0 THEN restore,'./irdc_dist_model/bgps_rb3.sav',/ver
restore,'./irdc_dist_model/data/case2_irdc_TdFIT.sav',/ver


;; Print Table header to file

printf,lun,'%======================================================='
printf,lun,'%  Source Listing Table'
printf,lun,'%        TAG: table:sources'
printf,lun,'%======================================================='
printf,lun,''
printf,lun,'\begin{deluxetable}{ccccccrrccccrr}'
printf,lun,'  \tablecolumns{14}'
printf,lun,'  \tablewidth{0pc}'
printf,lun,'  \tabletypesize{\scriptsize}'
printf,lun,'  \rotate'
printf,lun,'  \tablecaption{Source List, Observed \& Derived Properties\label{table:sources}}'
printf,lun,'  \tablehead{'
printf,lun,'    \multicolumn{4}{c}{BGPS Source Properties\tablenotemark{a}} & \colhead{IRDC} &'
printf,lun,'    \colhead{} & \multicolumn{2}{c}{Kin. Dist.\tablenotemark{\dag}} & \colhead{} &'
printf,lun,'    \multicolumn{2}{c}{KDA Resolution} & \colhead{} & '
printf,lun,'    \multicolumn{2}{c}{$N(H_2)$\tablenotemark{\ddag}} \\'
printf,lun,'    '
printf,lun,'    \cline{1-4} \cline{7-8} \cline{10-11} \cline{13-14}'
printf,lun,'    \colhead{CNUM} & \colhead{$\ell$} & \colhead{$b$} &'
printf,lun,'    \colhead{$S_{40}$\tablenotemark{b}} & \colhead{Contrast} &'
printf,lun,'    \colhead{} & \colhead{Near} & \colhead{Far} & \colhead{} &'
printf,lun,'    \colhead{IRDC} & \colhead{Morph} & \colhead{} & '
printf,lun,'    \colhead{BGPS} & \colhead{IRDC} \\'
printf,lun,' '
printf,lun,'    \colhead{} & \colhead{(\degr)} & \colhead{(\degr)} &'
printf,lun,'    \colhead{(Jy)} & \colhead{} &'
printf,lun,'    \colhead{} & \multicolumn{2}{c}{(kpc)} & \colhead{} & '
printf,lun,'    \colhead{Model} & \colhead{Match} & \colhead{} & '
printf,lun,'    \multicolumn{2}{c}{($\times 10^{21}$ cm$^{-2}$)}'
printf,lun,'  }'
printf,lun,'  \startdata'


;;============
;; DATA TO GO HERE... Only do first 20 or so...

if n_elements(nh2_irdc) EQ 0 THEN BEGIN
   nh2_irdc = dblarr(n)
   nh2_bgps = dblarr(n)
   sig_ni   = dblarr(n)
   sig_nb   = dblarr(n)
ENDIF

;; Place into decent units
nh2_irdc /= 1.d21 
nh2_bgps /= 1.d21
sig_ni   /= 1.d21
sig_nb   /= 1.d21

kdf /= 1.d3
kdn /= 1.d3

inf = strarr(n_elements(rb3))
neari = WHERE(~nearfar)
fari  = WHERE(nearfar)
inf[neari] = 'N'
inf[fari]  = 'F'
consi = WHERE(~constrain)
inf[consi] = 'U'

FOR i=0L,19 DO BEGIN
   
   r = rb3[i]
   IF r.b GE 0 THEN pms = '\phm{-}' ELSE pms = ''
   
   str = string(r.cnum,r.l,pms,r.b,r.s_peak/1.d3,r.s_err/1.d3,r.c_meas,$
                r.sig_c,kdn[i],kdf[i],inf[i],nh2_bgps[i],sig_nb[i],$
                nh2_irdc[i],sig_ni[i],format=$
                "(I4,' & ',F0.3,' & ',A0,F0.3,' & $',F0.3,"+$
                "'\pm ',F0.3,'$ & $',F0.3,'\pm ',F0.3,'$ & & ',F0.1,' & ',"+$
                "F0.1,' & & ',A0,' & N/F & & $',F0.1,' \pm ',F0.1,'$ & "+$
                "$',F0.1,' \pm ',F0.1,'$ \\')")
   printf,lun,str
   
   
ENDFOR

printf,lun,'  \enddata'
printf,lun,'  \tablenotetext{a}{\citet{Rosolowsky:2010}}'
printf,lun,'  \tablenotetext{b}{Flux density and uncertainty within a 40\arcsec\ aperture, corrected by the factor of $1.5\pm0.15$ from \citet{Aguirre:2011}}'
printf,lun,'  \tablenotetext{\dag}{Kinematic distances derived from molecular spectroscopy of \citet{Shirley:2012,Schlingman:2011,Dunham:2010,Dunham:2011c} and the Galacitc rotation curve of \citet{Reid:2009}.}'
printf,lun,'  \tablenotetext{\ddag}{Column densities derived from millimeter flux density and mid-IR extinction, respectively (see \S \ref{res:column}).}'
printf,lun,'  \tablecomments{Table \ref{table:sources} is published in its entirety in a machine-readable format in the online journal.  A portion is shown here for guidance regarding its form and content.}'
printf,lun,'\end{deluxetable}'


;; Clean up
close,lun
free_lun,lun


END




